package gold.digger;

import gold.vo.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC94 {

    /*
     * @param 此题目直接用别人代码，自己只理解思想
     * 中旬遍历二叉树的非递归实现，需要背下来
     * @return:
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while (root != null || !stack.empty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            if (!stack.isEmpty()) {
                root = stack.pop();
                res.add(root.val);
                root = root.right;
            }
        }

        return res;
    }


    public static void main(String[] args) {
        LC94 an = new LC94();
        TreeNode root = TreeNode.createTree("1,null,2,3");
        System.out.println(root);
    }
}
